<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb">
								 <li>
									 <a onclick="getOne()">顶级分类列表</a>
								 </li>
                        		<li>
		                        	<a>
										<select class="one" onchange="getTwo(this.value)">
											<option value="-1">请选择</option>
										</select>
									</a>
		                        </li>
		                        <li>
									<a>
										<select class="two" onchange="getThree(this.value)">
											<option value="-1">请选择</option>
										</select>
									</a>
		                        </li>
		                        <!--<li>
									<a>
										<select class="three">
											<option value="-1">请选择</option>
										</select>
									</a>
		                        </li>-->
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteCheck()" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>
							<div class="box-tools pull-right">
								<!--<div class="has-feedback">
									品牌名称：<input type="text" id="name" name="name">
									<button onclick="searchName()" class="btn btn-default">查询</button>
								</div>-->
							</div>
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemTable">
			                          <tr>
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>982</td>
									      <td>吊坠/项链</td>									    
									      <td>11</td>
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							共<span id="pages">12</span>页<span id="total">132</span>条
							<div style="float: right">
							<nav>
								<ul class="pagination" id="pageNum">
									<li><a href="">首页</a></li>
									<li class="threeword"><a href="#">上一页</a></li>
									<li><a href="#">1</a></li>
									<li><a href="#">2</a></li>
									<li><a href="#">3</a></li>
									<li><a href="#">4</a></li>
									<li><a href="#">5</a></li>
									<li><a href="#">6</a></li>
									<li><a href="#">7</a></li>
									<li><a href="#">8</a></li>
									<li><a href="#">9</a></li>
									<li><a href="#">10</a></li>
									<li class="threeword"><a href="javascript:">下一页</a></li>
									<li class="threeword"><a href="javascript:">末页</a></li>
								</ul>
							</nav>
							</div>
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
						<select class="one" id="addOne" name="parentId" onchange="getTwo(this.value)">
							<option value="-1">请选择</option>
						</select>
						<select class="two" id="addTwo" name="parentId" onchange="getThree(this.value)">
							<option value="-1">请选择</option>
						</select>
						<!--<select class="three">
							<option value="-1">请选择</option>
						</select>-->
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" id="name" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      			<input select2 config="options['type_template']" placeholder="商品类型模板" id="type_id" class="form-control" type="text"/>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="addItem()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <input type="hidden" name="id" id="id">
				  <table class="table table-bordered table-striped"  width="800px" id="updateTable">
					  <tr>
						  <td>上级商品分类</td>
						  <td>
							  <select class="one" id="updateOne" name="parentId" onchange="getTwo(this.value)">
								  <option value="-1">请选择</option>
							  </select>
							  <select class="two" id="updateTwo" name="parentId" onchange="getThree(this.value)">
								  <option value="-1">请选择</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input  class="form-control" id="updateName" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <input select2 config="options['type_template']" placeholder="商品类型模板" id="updateTypeid" class="form-control" type="text"/>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updateItem()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

<script>
	$(function () {
		getOne()
	})
	function getOne() {
		$.get("/itemController/getOne",{id:0},function (data) {
			var data1 = data.data;
			var op = '<option value="-1">请选择</option>';
			for (let i = 0; i < data1.length ; i++) {
				op += '<option value="'+data1[i].id+'">'+data1[i].name+'</option>'
			}
			$(".one").html(op)
			$(".two").html('<option value="-1">请选择</option>')
			getItem(1,10,0)
		})
	}
	function getTwo(value) {
		$.get("/itemController/getOne",{id:value},function (data) {
			var data1 = data.data;
			var op = '<option value="-1">请选择</option>';
			for (let i = 0; i < data1.length ; i++) {
				op += '<option value="'+data1[i].id+'">'+data1[i].name+'</option>'
			}
			$(".two").html(op)
			$("#three").html('<option value="-1">请选择</option>')
			getItem(1,10,value)
		})
	}
	function getThree(value) {
		$.get("/itemController/getOne",{id:value},function (data) {
			var data1 = data.data;
			var op = '<option value="-1">请选择</option>';
			for (let i = 0; i < data1.length ; i++) {
				op += '<option value="'+data1[i].id+'">'+data1[i].name+'</option>'
			}
			$(".three").html(op)
			getItem(1,10,value)
		})
	}
	function getItem(pageNum,pageSize,parentId) {
		$.ajax({
			url:"/itemController/findAll",
			type: "POST",
			dataType: "json",
			data: {pageNum:pageNum,pageSize:pageSize,parentId:parentId},
			async: false,
			success : function (data) {
				console.log(data)
				//  展示总页码和总记录数
				$("#pages").html(data.data.pages)
				$("#total").html(data.data.total)
				//拼接数据
				var table = '';
				var rows = data.data.rows;
				for (let i = 0; i < rows.length; i++) {
					tr = '<tr>' +
							'<td><input name="id" value="'+rows[i].id+'"  type="checkbox"></td>' +
							'<td>'+rows[i].id+'</td>'+
							'<td>'+rows[i].name+'</td>'+
							'<td>'+rows[i].typeId+'</td>'+
							'<td class="text-center">' +
							'<button type="button" onclick="getTwo('+rows[i].id+')" class="btn bg-olive btn-xs" >查询下级</button>' +
							'<button type="button" onclick="editItem('+rows[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal1" >修改</button>' +
							'</td>' +
							'</tr>'
					table += tr;
				}
				$("#itemTable").html(table);

				//  展示分页
				var lis = "";
				var fristPage = '<li onclick="javascipt:getItem('+1+','+10+','+parentId+')"><a href="javascript:void(0)">首页</a></li>';
				//判断上一个是否能继续执行
				var beforeNum = data.data.pageNum;
				if(beforeNum <= 0){
					beforeNum = 1;
				}
				var beforePage = '<li onclick="javascipt:getItem('+(beforeNum-1)+','+10+','+parentId+')"><a href="javascript:void(0)">上一页</a></li>';
				lis += fristPage;
				lis += beforePage;

				// 定义开始位置begin,结束位置 end
				var begin; // 开始位置
				var end ; //  结束位置
				//1.要显示10个页码
				if(data.data.pages < 10){
					//总页码不够10页
					begin = 1;
					end = data.data.pages;
				}else{
					//总页码超过10页
					begin = data.data.pageNum - 5 ;
					end = data.data.pageNum + 4 ;
					//2.如果前边不够5个，后边补齐10个
					if(begin < 1){
						begin = 1;
						end = begin + 9;
					}
					//3.如果后边不足4个，前边补齐10个
					if(end > data.data.pages){
						end = data.data.pages;
						begin = end - 9 ;
					}
				}
				for (let i = begin; i <= end; i++) {
					//判断当前页码是否选中,给他加一个样式
					var li ;
					if (data.data.pageNum == i){
						li = '<li class="active" onclick="javascipt:getItem('+1+','+10+','+parentId+')")"><a href="javascript:void(0)">'+i+'</a></li>';
					}else {
						li = '<li onclick="javascipt:getItem('+i+','+10+','+parentId+')")"><a href="javascript:void(0)">'+i+'</a></li>';
					}
					lis += li;
				}
				//判断下一个是否能继续执行
				var lastNum = data.data.pageNum+1;
				if(lastNum == data.data.pages){
					lastNum = data.data.pages;
				}
				var nextPage = '<li  onclick="javascipt:getItem(' + lastNum + ',' + 10 +','+parentId+')" class="threeword"><a href="javascript:void(0)">下一页</a></li>';
				var lastPage = '<li onclick="javascipt:getItem('+data.data.pages+','+10+','+parentId+')"><a href="javascript:void(0)">末页</a></li>';
				lis += nextPage;
				lis += lastPage;
				$("#pageNum").html(lis)

			},error : function (data) {
				alert(data.message);
			}
		})
	}

	//添加分类
	function addItem(){
		var name = $("#name").val();
		var typeId = $("#type_id").val();
		var addTwo = $("#addTwo").val();
		var parentId ;
		if (addTwo == -1){
			var addOne = $("#addOne").val();
			parentId = addOne;
		}else {
		parentId = addTwo;
		}

		$.get("/itemController/addItem",{parentId:parentId,name:name,typeId:typeId},function (data) {
			if (data.code == 10000){
				alert(data.message);
				window.location.reload();
			}else {
				alert(data.message);
			}
		})

	}

	//回显
	function editItem(id) {
		$.get("/itemController/editItem",{id:id},function (data) {
			console.log(data)
			if (data.code == 10000){
				var data1 = data.data;
				/*var table = '';
				table += '<tr>'
				table +='<td>上级商品分类</td>'
				table +='<td>'
				table +='<select class="one" id="updateOne" name="parentId" onchange="getTwo(this.value)">' +
						'<option value="-1">请选择</option>' +
						'</select>'

				table +='<select class="two" id="updateTwo" name="parentId" onchange="getThree(this.value)">'
				table +='<option value="-1">请选择</option>'
				table +='</select>'
				table +='</td>'
				table +='</tr>'
				table +='<tr>'
				table +='<td>商品分类名称</td>'
				table +='<td><input  class="form-control" id="updateName" name="name" value="'+data1.name+'" placeholder="商品分类名称">  </td>'
				table +='</tr>'
				table +='<tr>'
				table +='<td>类型模板</td>'
				table +='<td>'
				table +='<input select2 config="options[\'type_template\']" placeholder="商品类型模板" id="updateTypeid" name="typeId" value="'+data1.typeId+'" class="form-control" type="text"/>'
				table +='</td>'
				table +='</tr>';
				$("#updateTable").html(table);*/
				$("#id").val(data1.id)
				$("#updateName").val(data1.name)
				$("#updateTypeid").val(data1.typeId)
			}else {
				alert(data.message);
			}
		})
	}

	//修改
	function updateItem() {
		var id = $("#id").val();
		var name = $("#updateName").val();
		var typeId = $("#updateTypeid").val();
		var addTwo = $("#updateTwo").val();
		var parentId ;
		if (addTwo == -1){
			var addOne = $("#updateOne").val();
			parentId = addOne;
		}else {
			parentId = addTwo;
		}
		$.get("/itemController/updateItem",{id:id,name:name,typeId:typeId,parentId:parentId},function (data) {
			if (data.code == 10000){
				alert(data.message)
				window.location.reload();
			}else {
				alert(data.message)
			}
		})
	}

	//批量删除
	function deleteCheck() {
		let ids = [];
		let idsArr = document.getElementsByName("id");
		for(let i=0; i<idsArr.length; i++){
			if(idsArr[i].checked){
				ids.push(idsArr[i].value);
			}
		}
		if (ids.length > 0){
			let flag = confirm("您确定要删除吗?");
			if (flag){
				$.get("itemController/deleteItem","ids="+ids,function (data) {
					if (data.code == 10000){
						alert("删除成功");
						window.location.reload();
					}else {
						alert("删除失败");
					}
				})
			}else {
				alert("删除失败");
			}
		}else {
			alert("请您选择要删除的行");
		}

	}
</script>
</body>
</html>